//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Text;
using System.Web.UI;
using CommunityServer.Components;
using TWC = Telligent.Web.UI;

namespace CommunityServer.Controls
{
	public abstract class ClientPopulatedPopupMenu : TemplatedWebControl
	{
		public ClientPopulatedPopupMenu(): base()
		{
			this.SkinName = "Skin-ClientPopulatedPopupMenu.ascx";
		}

		#region Child Controls
		
		protected TWC.PopupMenu menu;

		#endregion
		
		#region Skin

		protected override void AttachChildControls() 
		{
			menu = (TWC.PopupMenu) FindControl("Menu");
		}

		#endregion

		#region Control Event Handlers

		protected override void OnLoad(EventArgs e)
		{
            this.EnsureChildControls();

			menu.Visible = false;

			if (this.Context.Items["ClientPopulatedPopupMenu_" + this.SkinName + "_Menu"] == null)
			{
				this.Context.Items.Add("ClientPopulatedPopupMenu_" + this.SkinName + "_Menu", menu);
				menu.Visible = true;
			}
			else
				menu = (TWC.PopupMenu) this.Context.Items["ClientPopulatedPopupMenu_" + this.SkinName + "_Menu"];

			base.OnLoad(e);
		}

		protected override void OnPreRender(EventArgs e)
		{
			if (!this.Page.IsClientScriptBlockRegistered("CommunityServer.Controls.ClientPopulatedPopupMenu"))
				this.Page.RegisterClientScriptBlock("CommunityServer.Controls.ClientPopulatedPopupMenu", string.Format("<script type=\"text/javascript\" src=\"{0}\"></script>",this.ResolveUrl("~/utility/ClientPopulatedPopupMenu.js")));

			if (!this.Page.IsStartupScriptRegistered(this.ClientID))
			{
			    StringBuilder sb = new StringBuilder();
				sb.Append("[");
				foreach (ClientMenuItemFormat format in this.ClientMenuItemFormats)
				{
					if (sb.Length > 1)
						sb.Append(",");

					sb.Append("[");
			
					sb.Append(format.ClientSideDisplayFunction);
					sb.Append(",");
					sb.Append(FormatJavaScript(format.Text, true));
					sb.Append(",");
					sb.Append(FormatJavaScript(format.Icon, true));
					sb.Append(",");
					sb.Append(FormatJavaScript(format.NavigateUrl, true));
					sb.Append(",");
					sb.Append(FormatJavaScript(format.Target, true));
					sb.Append(",");
					sb.Append(FormatJavaScript(format.ClientSideCommand, true));
					

					sb.Append("]");
				}
				sb.Append("]");

				Page.RegisterStartupScript(this.ClientID, String.Format("<script language=\"javascript\">window.{0} = new ClientPopulatedPopupMenu('{0}','{1}','{2}','{3}',{4});</script>", this.ClientID, this.menu.ClientID, this.InactiveButtonCssClass, this.ActiveButtonCssClass, sb.ToString()));
			}

			base.OnPreRender(e);
		}

		#endregion

		#region Helper Methods

		protected string FormatJavaScript(string value, bool wrapInQuotes)
		{
			if (Globals.IsNullorEmpty(value))
			{
				return "null";
			}
			
			value = value.Replace("\\", "\\\\");
			value = value.Replace("'", "\\'");

			if (wrapInQuotes)
				return "'" + value + "'";
			else
				return value;
		}

		protected string GetOpenPopupMenuScript(string menuParameterJavaScript, params string[] parameters)
		{
			StringBuilder sb = new StringBuilder();
			sb.Append("[");
			for (int i = 0; i < parameters.Length; i++)
			{
				if (sb.Length > 1)
					sb.Append(",");

				sb.Append("'");
				if (parameters[i] != null)
				{
					sb.Append(parameters[i].Replace("'", "\\'"));
				}
				sb.Append("'");
			}
			sb.Append("]");

			return string.Format("window.{0}.LoadMenu(this,event,{1},{2});", this.ClientID, sb.ToString(), Globals.IsNullorEmpty(menuParameterJavaScript) ? "null" : menuParameterJavaScript);
		}

		protected string GetOpenPopupMenuScript()
		{
			return GetOpenPopupMenuScript(this.MenuParameterJavaScript, this.Parameters);
		}

		#endregion

		#region Properties

		abstract protected ArrayList ClientMenuItemFormats
		{
			get;
		}

		protected string[] Parameters
		{ 
			get 
			{
				object state = this.ViewState["ParameterString"];
				if (state == null)
					return new string[0];
				else
					return (string[]) state;
			}
			set
			{
				ViewState["ParameterString"] = value;
			}
		}

		protected string MenuParameterJavaScript
		{
			get
			{
				object state = this.ViewState["MenuParameterJavaScript"];
				if (state == null)
					return "null";
				else
					return (string) state;
			}
			set
			{
				ViewState["MenuParameterJavaScript"] = value;
			}
		}

		public override string ClientID
		{
			get
			{
				EnsureChildControls();
				return this.menu.ClientID + "_" + this.GetType().Name;
			}
		}

		public virtual string InactiveButtonCssClass
		{
			get
			{
				string state = (string) this.ViewState["InactiveButtonCssClass"];
				if (state == null)
					return "";
				else
					return state;
			}
			set
			{
				this.ViewState["InactiveButtonCssClass"] = value;
			}
		}

		public virtual string ActiveButtonCssClass
		{
			get
			{
				string state = (string) this.ViewState["ActiveButtonCssClass"];
				if (state == null)
					return "";
				else
					return state;
			}
			set
			{
				this.ViewState["ActiveButtonCssClass"] = value;
			}
		}

		public virtual string InnerButtonCssClass
		{
			get
			{
				string state = (string) this.ViewState["InnerButtonCssClass"];
				if (state == null)
					return "";
				else
					return state;
			}
			set
			{
				this.ViewState["InnerButtonCssClass"] = value;
			}
		}

		protected string Text
		{
			get
			{
				string state = (string) this.ViewState["Text"];
				if (state == null)
					return "";
				else
					return state;
			}
			set
			{
				this.ViewState["Text"] = value;
			}
		}

		protected string ResourceName
		{
			get
			{
				string state = (string) this.ViewState["ResourceName"];
				if (state == null)
					return "";
				else
					return state;
			}
			set
			{
				this.ViewState["ResourceName"] = value;
			}
		}

		protected string ResourceFileName
		{
			get
			{
				string state = (string) this.ViewState["ResourceFileName"];
				if (state == null)
					return "";
				else
					return state;
			}
			set
			{
				this.ViewState["ResourceFileName"] = value;
			}
		}

		#endregion

		#region Render Method

		protected override void Render(HtmlTextWriter writer)
		{
			writer.Write("<a href=\"#\" onclick=\"return ");
			writer.Write(this.GetOpenPopupMenuScript());
			writer.Write("\" class=\"");
			writer.Write(this.InactiveButtonCssClass);
			writer.Write("\" style=\"");
			
			foreach (String key in this.Style.Keys)
			{
				writer.Write(key);
				writer.Write(":");
				writer.Write(this.Style[key]);
				writer.Write(";");
			}

			writer.Write("\"><span class=\"");
			writer.Write(this.InnerButtonCssClass);
			writer.Write("\">");

			if (!Globals.IsNullorEmpty(this.ResourceName))
				writer.Write(ResourceManager.GetString(this.ResourceName, this.ResourceFileName));
			else
				writer.Write(this.Text);	
			
			writer.Write("</span></a>");

			base.Render(writer);
		}

		#endregion
	}

	public class ClientMenuItemFormat
	{
		public string ClientSideDisplayFunction = String.Empty;

		public string Text = String.Empty;
		public string Icon = null;
		public string NavigateUrl = String.Empty;
		public string ClientSideCommand = String.Empty;
		public string Target = String.Empty;

		public ClientMenuItemFormat()
		{
		}
	}
}



